<?php

abstract class AbstractParser
{
    protected $_html = '';
    protected $_url = '';
    protected $_allowed_tags = '';


    public function setUrl($url)
    {
        $this->_url = $url;
    }

    public function getUrl()
    {
        return $this->_url;
    }

    abstract public function getData($html);
    abstract public function getNextPage($html);
    abstract public function getList($html);
    abstract public function getItem($html);




    /**
     * �������� �������� ������� ����� ������
     *
     * @param string $text
     * @return string
     */
    protected function _cutSpaces( $text )
    {
        $text = mb_eregi_replace('>[\r\n\s]*<', '><', $text);
        $text = mb_eregi_replace('[\r\n\s]+', ' ', $text);
        $text = mb_eregi_replace('>[\r\n\s]+', '>', $text);
        $text = mb_eregi_replace('[\r\n\s]+<', '<', $text);
        return $text;
    }

    /**
     * �������� ����������� ���� � ������
     * � ����� ���������� ����������� ��������
     *
     * @param string $text
     * @return string
     */
    protected function _stripTags( $text )
    {
        $text = strip_tags( $text, $this->_allowed_tags );
        $text = $this->_stripTagsAttributes($text, $this->_allowed_tags, 'src,href,type');
        $text = mb_eregi_replace('(&#?\w+;)*', '', $text);
        //$text = mb_eregi_replace('[��]*', '', $text);
        $text = mb_eregi_replace('\s{2,}|\t+|\n+', ' ', $text);

        return $text;
    }

    /**
     * � ����� ���������� ����������� ��������
     *
     * @param string $string
     * @param string | array $allowtags
     * @param string | array $allowattributes
     * @return string
     */
    protected function _stripTagsAttributes( $string, $allowtags=NULL, $allowattributes=NULL )
    {
        $string = strip_tags($string,$allowtags);
        if (!is_null($allowattributes)) {
            if(!is_array($allowattributes))
                $allowattributes = explode(",",$allowattributes);
            if(is_array($allowattributes))
                $allowattributes = implode(")(?<!",$allowattributes);
            if (strlen($allowattributes) > 0)
                $allowattributes = "(?<!".$allowattributes.")";
            $string = preg_replace_callback("/<[^>]*>/i",create_function(
                '$matches',
                'return preg_replace("/ [^ =]*'.$allowattributes.'=(\"[^\"]*\"|\'[^\']*\')/i", "", $matches[0]);'
            ),$string);
        }

        return $string;
    }

    /**
     * ������ � ������ �� ��������
     *
     * @param string $pattern
     * @param string $text
     * @return array | boolean
     */
    protected function _match( $pattern, $text )
    {
        $matched = array();

        if( !preg_match_all('|' . $pattern . '|iU', $text, $matched) )
            return false;

        return $matched;
    }
}